/**
 * Copyright &copy; 2012-2016 <a href="https://shop450532966.taobao.com/">NumberOne</a> All rights reserved.
 */
package com.numberone.master.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.config.Global;
import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.sys.entity.NoticeInfo;
import com.numberone.master.modules.sys.service.NoticeInfoService;

/**
 * 公告管理Controller
 * @author ck
 * @version 2017-07-06
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/noticeInfo")
public class NoticeInfoController extends BaseController {

	@Autowired
	private NoticeInfoService noticeInfoService;

	 /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
	@ModelAttribute
	public NoticeInfo get(@RequestParam(required=false) String id) {
		NoticeInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = noticeInfoService.get(id);
		}
		if (entity == null){
			entity = new NoticeInfo();
		}
		return entity;
	}

	 /**
     * list列表
     * @param noticeInfo
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("sys:noticeInfo:list")
	@RequestMapping(value = {"list", ""})
	public String list(NoticeInfo noticeInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<NoticeInfo> page = noticeInfoService.findPage(new Page<NoticeInfo>(request, response), noticeInfo); 
		model.addAttribute("page", page);
		return "modules/sys/noticeInfoList";
	}

	/**
     * 打开增加页面
     * @return
     */
	@RequiresPermissions("sys:noticeInfo:save")
	@RequestMapping(value = "openSave")
	public String openSave() {
		return "modules/sys/noticeInfoSave";
	}


	/**
	 * 打开修改页面
	 * @param noticeInfo
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:noticeInfo:update")
	@RequestMapping(value = "openUpdate")
	public String openUpdate(NoticeInfo noticeInfo, Model model) {
		model.addAttribute("noticeInfo", noticeInfo);
		return "modules/sys/noticeInfoUpdate";
	}

	/**
	 * 添加
	 * @param noticeInfo
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("sys:noticeInfo:save")
	@RequestMapping(value = "save")
	public String save(NoticeInfo noticeInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, noticeInfo)){
			return openSave();
		}
		noticeInfoService.save(noticeInfo);
		addMessage(redirectAttributes, "添加公告信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/noticeInfo/?repage";
	}

	/**
	 * 修改
	 * @param noticeInfo
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("sys:noticeInfo:update")
	@RequestMapping(value = "update")
	public String update(NoticeInfo noticeInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, noticeInfo)){
			return openUpdate(noticeInfo, model);
		}
		noticeInfoService.update(noticeInfo);
		addMessage(redirectAttributes, "修改公告信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/noticeInfo/?repage";
	}

	 /**
     * 删除
     * @param noticeInfo
     * @param redirectAttributes
     * @return
     */
	@RequiresPermissions("sys:noticeInfo:delete")
	@RequestMapping(value = "delete")
	public String delete(NoticeInfo noticeInfo, RedirectAttributes redirectAttributes) {
		noticeInfoService.delete(noticeInfo);
		addMessage(redirectAttributes, "删除公告信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/noticeInfo/?repage";
	}

}